package com.myaihui.service.impl;

import com.myaihui.domain.CarDossier;
import com.myaihui.domain.PropertyMgr;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.model.park.response.CarOwner;
import com.myaihui.foundation.query.CarDossierQuery;
import com.myaihui.repository.CarDossierRepository;
import com.myaihui.repository.PropertyMgrRepository;
import com.myaihui.service.CarDossierService;
import com.myaihui.service.ParkApi;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.myaihui.foundation.constant.SysUserConstant.MARKET_ADMIN;
import static com.myaihui.foundation.constant.SysUserConstant.PM_ADMIN;

@Service
public class CarDossierServiceImpl implements CarDossierService {

    @Autowired
    private CarDossierRepository carDossierRepository;

    @Autowired
    private PropertyMgrRepository propertyMgrRepository;

    @Autowired
    private ParkApi parkApi;

    @Override
    public Pager<CarDossier> findByConditions(CarDossierQuery carQuery) {
        Specification<CarDossier> specification = new Specification<CarDossier>() {
            @Override
            public Predicate toPredicate(Root<CarDossier> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(carQuery.getCarNo()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("carNo"), "%" + carQuery.getCarNo() + "%")));

                if (Objects.nonNull(carQuery.getStatus()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), carQuery.getStatus())));

                if (carQuery.getType() == PM_ADMIN)
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("propertyMgrId"), carQuery.getPlatformId())));

                if (carQuery.getType() == MARKET_ADMIN)
                    predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("marketId"), carQuery.getPlatformId())));

                query.where(predicates.toArray(new Predicate[predicates.size()]));
                query.orderBy(criteriaBuilder.asc(root.get("status")), criteriaBuilder.desc(root.get("created")));
                return query.getRestriction();
            }
        };
        Pageable pageable = new PageRequest(carQuery.getiDisplayStart(), carQuery.getiDisplayLength());
        Page<CarDossier> page = carDossierRepository.findAll(specification, pageable);
        return new Pager(page, carQuery.getsEcho(), page.getContent());
    }

    @Override
    public CarDossier detail(String id) {
        return carDossierRepository.findOne(id);
    }

    @Override
    public CarDossier audit(String carId, Integer status, String remark) {
        CarDossier carDossier = carDossierRepository.findOne(carId);
        carDossier.setStatus(status);
        carDossierRepository.save(carDossier);

        if (status == 1){
            PropertyMgr propertyMgr = propertyMgrRepository.findOne(carDossier.getPropertyMgrId());
            parkApi.createMonthCarUser(propertyMgr.getParkId(), CarOwner.of(carDossier));
        }
        return carDossier;
    }
}
